# APKBUILD based on linux-vanilla aport. Changes:
# - add !check !tracedeps
# - add generation of master DTB with dtbTool
#
# Kernel config based on: arch/arm/configs/msm8610_defconfig
# Changes:
# - apply patches to compile with gcc6
# - enable devtmpfs (needed for udev -> touch support in weston)
# - disable input mouse driver
# - enable generation of dtb

_vendor=huawei
_flavor=huawei-y530
_config="config-${_flavor}.armhf"
_repository="android_kernel_huawei_msm8x1x"
_hash="1880463bf10c4c2d2b348698614a613902184e22"

pkgname=linux-${_flavor}
pkgver=3.4.0
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=2
arch="armhf"
pkgdesc="Kernel for Huawei MSM8x1x devices"
url="https://github.com/PieroV/${_repository}"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev dtbtool"
options="!strip !check !tracedeps"
install=
source="
	$pkgname-$_hash.tar.gz::${url}/archive/${_hash}.tar.gz
	$_config
	compiler-gcc6.h
	00_fix_return_address.patch
	01_timeconst_fix.patch
	02_gpu-msm-fix-gcc5-compile.patch
	03_avoid_rpmb_read_during_boot.patch
	04_add_missing_dtb.patch
	05_fix_mdp3_ctrl_off.patch
	06_fix_mdss_fb_rgb_mode.patch
	07_fix_mdss_fb_refresh_rate.patch
	08_fix_touchscreen.patch
"
subpackages=""
license="GPL2"

_abi_release=${pkgver}
_carch="arm"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/${_repository}-${_hash}"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"
	make mrproper

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$ksrcdir/include/linux/"

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig
}



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build
	echo "--[ menuconfig ]--"
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS

	echo "--[ Build kernel ]--"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine" CONFIG_NO_ERROR_ON_MISMATCH=y

	echo "--[ Build modules ]--"
	# the 'no-pic' flag is necessary to avoid the
	# error 'Unknown symbol _GLOBAL_OFFSET_TABLE_ (err 0)' when loading the module
	make ARCH="$_carch" CC="${CC:-gcc}" CFLAGS_MODULE="-fno-pic" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-Alpine" CONFIG_NO_ERROR_ON_MISMATCH=y \
		modules

	echo "--[ Generate master DTB ]--"
	dtbTool -s 2048 -p "scripts/dtc/" -o "arch/arm/boot/dt.img" "arch/arm/boot/"
}

package() {
	echo "--[ Installing kernel ]--"
	install -Dm644 "$srcdir/build/arch/arm/boot/zImage" \
		"$pkgdir/boot/vmlinuz-$_flavor"

	echo "--[ Installing master DTB ]--"
	install -Dm644 "$srcdir/build/arch/arm/boot/dt.img" \
		"$pkgdir/boot/dt.img"

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	cd "$srcdir"/build
	unset LDFLAGS

	echo "--[ Installing modules ]--"
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1))-Alpine" CONFIG_NO_ERROR_ON_MISMATCH=y \
		INSTALL_MOD_PATH="$pkgdir" INSTALL_MOD_STRIP=1 modules_install
}

sha512sums="b88fe4981fb35d289977c9a7ca6b041ec8d09380250485d7b3f3e1e1ef51ba500db2e1aecd222aa8c140a3b9ec5ed2fa638b68ddf446efe81f278aa8b8e7871f  linux-huawei-y530-1880463bf10c4c2d2b348698614a613902184e22.tar.gz
cd782099e7174fc132aa35b96e9ffa5ee25f12821eaaecbcfafbd79d8a954ee2ae6d98caf4a6838de64e1f64253eebeacd6176c0478f517f9956d6fa9279c809  config-huawei-y530.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
ea1d3b5a234fa565e3c1a792de48f4fc4e6023d281d303c8e319c7ef28edc5739ab0e4dea0139a41f0a5c7d03e27921ccaa214fd0ac5c72245a094ce60128864  00_fix_return_address.patch
a2bb98fb8d988bbb659cae00fbaca360828300e9b98b90aed5ee0dd839c3f740696df4094a9021b813cbada06820d115aabed581a47cdd2c947e8d853c20b145  01_timeconst_fix.patch
7be03a9e78b7ac330a54b1f00509caa0621a95c0c55901878ad757f9dd69cc05ba2c8b5ea987063ae1224f92c4d090d515fa5d369e7755181a4871b0d0f82881  02_gpu-msm-fix-gcc5-compile.patch
760fdff79df31754ebed59839f34fc8c5a61750471614ac93a01b1e13b2535e7ca1f879c0cad3e83ad92946586c91679ba853eac4fe454d7e948317c84d81941  03_avoid_rpmb_read_during_boot.patch
42716cf894445fe02a46a1f26b2faa4d45ffea35e2dfc15acf5ce66fdd67d5faf8201b07ea10716c61c7f900561262731771c7b655f9e95ff7a9273de5746244  04_add_missing_dtb.patch
fbc61e379403990167853f4c4fbdfa22d7e9661a7eedecf2a75eec2a8008991a82d0a61be844ab4b1c88383f3fa79b43eff10b1039e44ad5a758a173cbf6386b  05_fix_mdp3_ctrl_off.patch
384ae0bf2f5d7e15b0b60655693fd600b52c0f5bfcaa4ba7336d0ebeb224e84b99b597f77d26a1249e14a4e7bbc1629be00556b393bb9dd2266cb4bd67c3cfea  06_fix_mdss_fb_rgb_mode.patch
1f743d3e7f4ac7401167ea35edc7d7e113bd80824c4d53631d7500f33557ff486163f8244786c16506b2e21db6ccbcd8a0f7b26fcf4937efee50b62c074f9a22  07_fix_mdss_fb_refresh_rate.patch
f83e9b6549155a2f32c89dffbab3ad6fba7b8d93976dfccb1232833244ed6e064e28105a321eaa934c5bb326384730d03f3d3141227a7258a30045fa12573268  08_fix_touchscreen.patch"
